<?
include_once $_SERVER['DOCUMENT_ROOT'].'/nabLib/lib.php';

//==========================================
//==========================================
// DataBase Library Functions
//==========================================
//==========================================


//==========================================
function dbValidateClientId($id)
//==========================================
{
	 if($id > 999 && $id < 10000) 
	  return(1);
	 else
	  return(0);
}
//==========================================
function  dbWriteSample($id,$year,$month,$day,$hour,$min,$sec,$pow,$pulse)
//==========================================
{
	    $file_suffix = "nab";
	    $fp        = $_SERVER['DOCUMENT_ROOT']."nabServer/data/"; 
	    //$fp = "data/";
	    
        // Log data 
        $file1 = $fp."data-".$id.'-'.$year.'-'.$month.'-'.$day.".".$file_suffix; 
        $out = fopen($file1, 'a');
        $data = $hour." ".$min." ".$sec." ".$pow." ".$pulse."\n";
        fwrite($out, $data); 
        fclose($out);

        // Update accumulated kWh per diem
        $ftemp = $fp."kwh-".$id.'-'.$year.'-'.$month.'-'.$day.".".$file_suffix; 
        $value = readCurrentValue($ftemp);
        $value = $value + $pulse;
        writeNewValue($ftemp,$value);
        // Update accumulated kWh per month
        $ftemp = $fp."kwh-".$id.'-'.$year.'-'.$month.".".$file_suffix; 
        $value = readCurrentValue($ftemp);
        $value = $value + $pulse;
        writeNewValue($ftemp,$value);
        // Update accumulated kWh per year
        $ftemp = $fp."kwh-".$id.'-'.$year.".".$file_suffix; 
        $value = readCurrentValue($ftemp);
        $value = $value + $pulse;
        writeNewValue($ftemp,$value);


        // Write latest power to file
        $file2 = $fp.'pow-'.$id.".".$file_suffix; 
        $out = fopen($file2, 'w');
        fwrite($out, $pow); 
        fclose($out); 

        // Write latest date and time to file
        $file3 = $fp.'time-'.$id.".".$file_suffix; 
        $out = fopen($file3, 'w');
        $data = $year."-"; fwrite($out, $data); 
        $data = $month."-";fwrite($out, $data); 
        $data = $day."   ";fwrite($out, $data); 
        $data = $hour.":"; fwrite($out, $data); 
        $data = $min.":";  fwrite($out, $data); 
        $data = $sec."\n"; fwrite($out, $data); 
        fclose($out); 	
        
	    return(0);
}






?>
